package mesweb.mesweb.controller.procedure;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.dto.Procedure.MesProduceProcedureBranchDto;
import mesweb.mesweb.entity.Procedure.MesProduceProcedureBranch;
import mesweb.mesweb.entity.Procedure.ModelProduceLineBranchProcedureLine;
import mesweb.mesweb.service.procedure.IMesProduceProcedureBranchService;
import mesweb.mesweb.service.procedure.IModelProduceLineBranchProcedureLineService;
import mesweb.mesweb.vo.procecdure.MesProduceProcedureBranchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 生产工序分支
 * @Author: jeecg-boot
 * @Date:   2021-06-26
 * @Version: V1.0
 */

@Api(tags="生产工序线分支")
@RestController
@RequestMapping("/mes/mesProduceProcedureBranch")
public class MesProduceProcedureBranchController {
	@Autowired
	private IMesProduceProcedureBranchService mesProduceProcedureBranchService;

	@Autowired
	private IModelProduceLineBranchProcedureLineService modelProduceLineBranchProcedureLineService;
	
	@GetMapping("/getAllProduceLineBranchs")
	@ApiOperation("获取全部数据列表")
	public CommonResult<?> getAllProduceLineBranchs(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
		List<MesProduceProcedureBranch> data = new ArrayList<>();
		QueryWrapper<MesProduceProcedureBranch> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isBlank(name) || name.equals("全部")) {
		}
		data = mesProduceProcedureBranchService.list(queryWrapper);
		return CommonResult.success(data);
	}

    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<MesProduceProcedureBranchVo> data = new ArrayList<>();
		QueryWrapper<MesProduceProcedureBranchVo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
        }
		data = mesProduceProcedureBranchService.getProduceProcedureBranch(queryWrapper);
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param mesProduceProcedureBranch
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="生产工序分支-分页列表查询", notes="生产工序分支-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(MesProduceProcedureBranchVo mesProduceProcedureBranch,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<MesProduceProcedureBranchVo> queryWrapper = new QueryWrapper<>();
		Page<MesProduceProcedureBranchVo> pageMesProduceProcedureBranchVo = new Page<MesProduceProcedureBranchVo>(page, size);
		IPage<MesProduceProcedureBranchVo> pageList = mesProduceProcedureBranchService.getProduceProcedureBranch(pageMesProduceProcedureBranchVo, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param mesProduceProcedureBranch
	 * @return
	 */
	@ApiOperation(value="生产工序分支-编辑", notes="生产工序分支-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody MesProduceProcedureBranch mesProduceProcedureBranch) {

		boolean result = false;
        if(mesProduceProcedureBranch.getId() == 0L) {
			QueryWrapper<MesProduceProcedureBranch> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("produce_line_branch_no", mesProduceProcedureBranch.getProduceLineBranchNo());
			queryWrapper.or().eq("produce_line_branch_name", mesProduceProcedureBranch.getProduceLineBranchName());
			MesProduceProcedureBranch mesTechnologyFlow1 = mesProduceProcedureBranchService.getOne(queryWrapper);
			if (mesTechnologyFlow1 != null) {
				String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getProduceLineBranchNo(), mesTechnologyFlow1.getProduceLineBranchName());
				throw new ApiException(msg);
			}
             result = mesProduceProcedureBranchService.save(mesProduceProcedureBranch);
        }
        else
        {
            result = mesProduceProcedureBranchService.updateById(mesProduceProcedureBranch);
        }
        return CommonResult.success(mesProduceProcedureBranch);
	}

	/**
	 * 编辑
	 *
	 * @param mesProduceProcedureBranchDto
	 * @return
	 */
	@ApiOperation(value="生产工序-编辑", notes="生产工序-编辑")
	@PostMapping(value = "/saveRelation")
	public CommonResult<?> saveRelation(@RequestBody MesProduceProcedureBranchDto mesProduceProcedureBranchDto) {
		boolean result = false;
		MesProduceProcedureBranch mesProduceProcedureBranch = new MesProduceProcedureBranch();
		BeanUtils.copyProperties(mesProduceProcedureBranchDto,mesProduceProcedureBranch);
		if(mesProduceProcedureBranchDto.getId() == 0L) {
			result = mesProduceProcedureBranchService.save(mesProduceProcedureBranch);
		}
		else
		{
			result = mesProduceProcedureBranchService.updateById(mesProduceProcedureBranch);
		}

		ModelProduceLineBranchProcedureLine modelProduceLineBranchProcedureLine = new ModelProduceLineBranchProcedureLine();
		modelProduceLineBranchProcedureLine.setProcedureLineId(mesProduceProcedureBranchDto.getProduceProcedureLineId());
		modelProduceLineBranchProcedureLine.setProduceProcedureBranchId(mesProduceProcedureBranch.getId());

		if( mesProduceProcedureBranchDto.getRelationId() != null  && mesProduceProcedureBranchDto.getRelationId()> 0) {
			modelProduceLineBranchProcedureLine.setId(mesProduceProcedureBranchDto.getRelationId());
			result = modelProduceLineBranchProcedureLineService.updateById(modelProduceLineBranchProcedureLine);
		}else{
			result = modelProduceLineBranchProcedureLineService.save(modelProduceLineBranchProcedureLine);
		}

		return CommonResult.success(modelProduceLineBranchProcedureLine);
	}


	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="生产工序分支-通过id删除", notes="生产工序分支-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		mesProduceProcedureBranchService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="生产工序分支-批量删除", notes="生产工序分支-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("MesProduceProcedureBranchIDs") @RequestBody List<Long> ids) {
		this.mesProduceProcedureBranchService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="生产工序分支-通过id查询", notes="生产工序分支-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		MesProduceProcedureBranch mesProduceProcedureBranch = mesProduceProcedureBranchService.getById(id);
		return CommonResult.success(mesProduceProcedureBranch);
	}
}
